//
//  CityListModel.m
//
//  Created by   on 2018/5/26
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "CityListModel.h"


NSString *const kCityListModelCode = @"code";
NSString *const kCityListModelId = @"id";
NSString *const kCityListModelSubCity = @"subCity";
NSString *const kCityListModelName = @"name";


@interface CityListModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation CityListModel

@synthesize code = _code;
@synthesize internalBaseClassIdentifier = _internalBaseClassIdentifier;
@synthesize subCity = _subCity;
@synthesize name = _name;

+ (NSArray<CityListModel *> *)parserRequestData:(NSArray<NSDictionary *> *)array
{
    if (![array isKindOfClass:[NSArray class]])
    {
        return nil;
    }
    NSMutableArray *resultArray = [NSMutableArray array];
    [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CityListModel *model = [CityListModel modelObjectWithDictionary:obj];
        [resultArray addObject:model];
    }];
    return resultArray;
}

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.code = [self objectOrNilForKey:kCityListModelCode fromDictionary:dict];
            self.internalBaseClassIdentifier = [self objectOrNilForKey:kCityListModelId fromDictionary:dict];
    NSObject *receivedCityListSubCity = [dict objectForKey:kCityListModelSubCity];
    NSMutableArray *parsedCityListSubCity = [NSMutableArray array];
    if ([receivedCityListSubCity isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedCityListSubCity) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedCityListSubCity addObject:[CityListModel modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedCityListSubCity isKindOfClass:[NSDictionary class]]) {
       [parsedCityListSubCity addObject:[CityListModel modelObjectWithDictionary:(NSDictionary *)receivedCityListSubCity]];
    }

    self.subCity = [NSArray arrayWithArray:parsedCityListSubCity];
            self.name = [self objectOrNilForKey:kCityListModelName fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.code forKey:kCityListModelCode];
    [mutableDict setValue:self.internalBaseClassIdentifier forKey:kCityListModelId];
    NSMutableArray *tempArrayForSubCity = [NSMutableArray array];
    for (NSObject *subArrayObject in self.subCity) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForSubCity addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForSubCity addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForSubCity] forKey:kCityListModelSubCity];
    [mutableDict setValue:self.name forKey:kCityListModelName];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.code = [aDecoder decodeObjectForKey:kCityListModelCode];
    self.internalBaseClassIdentifier = [aDecoder decodeObjectForKey:kCityListModelId];
    self.subCity = [aDecoder decodeObjectForKey:kCityListModelSubCity];
    self.name = [aDecoder decodeObjectForKey:kCityListModelName];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_code forKey:kCityListModelCode];
    [aCoder encodeObject:_internalBaseClassIdentifier forKey:kCityListModelId];
    [aCoder encodeObject:_subCity forKey:kCityListModelSubCity];
    [aCoder encodeObject:_name forKey:kCityListModelName];
}

- (id)copyWithZone:(NSZone *)zone
{
    CityListModel *copy = [[CityListModel alloc] init];
    
    if (copy) {

        copy.code = [self.code copyWithZone:zone];
        copy.internalBaseClassIdentifier = [self.internalBaseClassIdentifier copyWithZone:zone];
        copy.subCity = [self.subCity copyWithZone:zone];
        copy.name = [self.name copyWithZone:zone];
    }
    
    return copy;
}


@end
